/**
 * 
 */
package com.dycn.posdashboard.action;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

import org.apache.commons.lang.time.DateUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;

import com.dycn.posdashboard.data.JsonResponseData;
import com.dycn.posdashboard.data.PaginatedInfor;
import com.dycn.posdashboard.data.PosTransactionData;
import com.dycn.posdashboard.data.PosTransactionDataStats;
import com.dycn.posdashboard.data.ServicesResultObject;
import com.dycn.posdashboard.data.StatsTimeUnit;
import com.dycn.posdashboard.dbhelper.DataBaseUser;
import com.dycn.posdashboard.services.PosTradeServices;
import com.dycn.posdashboard.util.JSONObjectHelp;
import com.opensymphony.xwork2.ActionSupport;

/**
 * @author gongqiyi
 *
 */
public class PosTransactionDatasAction  extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 6434629048833189767L;

	protected final Log log = LogFactory.getLog(getClass());
    
	private PosTradeServices posTradeServices;
    private JsonResponseData<Object> commonResponseDataJson;
    
    public JsonResponseData<Object> getCommonResponseDataJson() {
		return commonResponseDataJson;
	}

	public void setCommonResponseDataJson(
			JsonResponseData<Object> commonResponseDataJson) {
		this.commonResponseDataJson = commonResponseDataJson;
	}

	public PosTradeServices getPosTradeServices() {
		return posTradeServices;
	}

	public void setPosTradeServices(PosTradeServices posTradeServices) {
		this.posTradeServices = posTradeServices;
	}

	/**
	 * 获取最新的Pos交易数据
	 * 只获取最新的20天数据内容
	 * @return
	 */
	public String getLiveRecentPosTransactionDatasOf(){
		commonResponseDataJson=new JsonResponseData<Object>();
		commonResponseDataJson.setSuccess(false);
		commonResponseDataJson.setResultCode(JsonResponseData.SUCCESS);
		commonResponseDataJson.setMsg("参数提交错误~~~!!!!!!");
		commonResponseDataJson.setData(null);
    	HttpServletRequest request = ServletActionContext.getRequest();  
    	String branchNo=request.getParameter("branchNo");//分公司编号
    	String terminalMerchantNo=request.getParameter("terNo");//终端商户编号
    	String cardNo=request.getParameter("cardNo");//卡号
//    	String checkDateRange=request.getParameter("timeRange");
//    	
//    	String startPageStr=request.getParameter("page");//想要返回的结果页码
//    	//String startRecordStr=request.getParameter("start");//开始的记录index
    	String limitstr=request.getParameter("limit");//每页记录数
//
//    	String limitstr=request.getParameter("limit");
//    	String totalRecordStr=request.getParameter("totalRecord");
    	
    	try{
    		//int pageSize=20;
	    	int startPage=0;

	    	int statsDateRangeType=0;//0-》全部 1 ->本月 2->本年
			//pageSize=Integer.parseInt(pageSizeStr);
	    	int pageSize=((limitstr==null||limitstr.trim().equals(""))?20:Integer.parseInt(limitstr));
	    	startPage=1;
	    	

				
				PaginatedInfor<PosTransactionData> nonDataPaginatedInfor=new PaginatedInfor<PosTransactionData>();
				nonDataPaginatedInfor.setPageId(startPage);
				nonDataPaginatedInfor.setPageRecords(pageSize);

				nonDataPaginatedInfor.setTotalRecords(20);
	
				
				//StatsTimeUnit statsTimeUnit
				Date startDateTime=DateUtils.truncate(new Date(),Calendar.HOUR_OF_DAY);
				Date endDateTime=null;
				if(statsDateRangeType==1){
					startDateTime=DateUtils.truncate(new Date(),Calendar.MONTH);
					endDateTime=null;
				}else if(statsDateRangeType==2){
					startDateTime=DateUtils.truncate(new Date(),Calendar.YEAR);
					endDateTime=null;
				}else{
					startDateTime=null;
					endDateTime=null;
				}

				ServicesResultObject<PaginatedInfor<PosTransactionData>> servicesResultObjectOfgetPosTransactionDataListBy=this.posTradeServices.getPosTransactionDataListBy(DataBaseUser.MAIN_USER, branchNo, terminalMerchantNo,cardNo,startDateTime, endDateTime, nonDataPaginatedInfor);
				
	    	    if(!servicesResultObjectOfgetPosTransactionDataListBy.isSuccess()){
	        		commonResponseDataJson.setSuccess(servicesResultObjectOfgetPosTransactionDataListBy.isSuccess());
	           		commonResponseDataJson.setMsg(servicesResultObjectOfgetPosTransactionDataListBy.getResultMsg());
	        		commonResponseDataJson.setData(null);
	        	}else{
	        		PaginatedInfor<PosTransactionData> posTransactionDataPageInfor=servicesResultObjectOfgetPosTransactionDataListBy.getReturnObj();
	         		  
	        		List<PosTransactionData> currrentPagePosTransactionDataList=posTransactionDataPageInfor.getDataList();
	        		int totalRecords=posTransactionDataPageInfor.getTotalRecords();
	        		int totalPages=1;
	    			if(totalRecords%pageSize!=0){
						totalPages=totalRecords/pageSize+1;
					}else{
						totalPages=totalRecords/pageSize;
					}
	    			
	    			if(startPage>totalPages){
	    				startPage=totalPages;
	    			}
	    			
	        		commonResponseDataJson.setMsg("");
	        		commonResponseDataJson.setSuccess(true);
	        		commonResponseDataJson.setResultCode(JsonResponseData.SUCCESS);
	        		commonResponseDataJson.setData(currrentPagePosTransactionDataList);
	        		
	        		commonResponseDataJson.setRecordsPer(pageSize);
	        		commonResponseDataJson.setPageId(startPage);
	        		commonResponseDataJson.setTotalRecords(totalRecords);
	        		commonResponseDataJson.setTotalPage(totalPages);
	        		
	          		log.debug("getETLJobEfforts json data22:"+JSONObject.fromObject(commonResponseDataJson,JSONObjectHelp.getJsonConfig()).toString());
	
	        	}
    		
    		
    	}catch(Exception exp){
    		exp.printStackTrace();
    		commonResponseDataJson.setResultCode(JsonResponseData.EXCEPTION);
    		commonResponseDataJson.setMsg(commonResponseDataJson.getMsg()+exp);
    	}
		
		return SUCCESS;
	}
	
	/**
	 * 查询Pos交易数据
	 * http://localhost:8080/pos-dashboard/getPosTransactionDatasOfSpecCard.action?page=1&&timeRange=2&&branchNo=906290053110002&&terNo=&&cardNo＝9800060812065858
	 * @return
	 */
	public String getPosTransactionDatasOfCard(){
		commonResponseDataJson=new JsonResponseData<Object>();
		commonResponseDataJson.setSuccess(false);
		commonResponseDataJson.setResultCode(JsonResponseData.SUCCESS);
		commonResponseDataJson.setMsg("参数提交错误~~~!!!!!!");
		commonResponseDataJson.setData(null);
    	HttpServletRequest request = ServletActionContext.getRequest();  
    	String branchNo=request.getParameter("branchNo");//分公司编号
    	String terminalMerchantNo=request.getParameter("terNo");//终端商户编号
    	String cardNo=request.getParameter("cardNo");//卡号
    	String checkDateRange=request.getParameter("timeRange");
    	
    	String startPageStr=request.getParameter("page");//想要返回的结果页码
    	//String startRecordStr=request.getParameter("start");//开始的记录index
    	//String pageSizeStr=request.getParameter("limit");//每页记录数

    	String limitstr=request.getParameter("limit");
    	String totalRecordStr=request.getParameter("totalRecord");
    	
    	try{
    		int pageSize=((limitstr==null||limitstr.trim().equals(""))?20:Integer.parseInt(limitstr));
	    	int startPage=0;

	    	int statsDateRangeType=0;//0-》全部 1 ->本月 2->本年
			//pageSize=Integer.parseInt(pageSizeStr);
	    	
	    	startPage=Integer.parseInt((startPageStr==null||startPageStr.trim().equals(""))?"1":startPageStr);
	    	
    		if(cardNo!=null&&!cardNo.trim().equals("")){
    			
				//pageSize=Integer.parseInt(pageSizeStr);
				startPage=Integer.parseInt(startPageStr);
		    		
				if(checkDateRange==null||checkDateRange.trim().equals("")){
		    		statsDateRangeType=0;
		    	}else {
		    		statsDateRangeType=Integer.parseInt(checkDateRange);
		    	}
				
				PaginatedInfor<PosTransactionData> nonDataPaginatedInfor=new PaginatedInfor<PosTransactionData>();
				nonDataPaginatedInfor.setPageId(startPage);
				nonDataPaginatedInfor.setPageRecords(pageSize);
				if(totalRecordStr==null||totalRecordStr.trim().equals("")){
					nonDataPaginatedInfor.setTotalRecords(-1);
				}else{
					nonDataPaginatedInfor.setTotalRecords(Integer.parseInt(totalRecordStr));
				}
				
				//StatsTimeUnit statsTimeUnit
				Date startDateTime=DateUtils.truncate(new Date(),Calendar.HOUR_OF_DAY);
				Date endDateTime=null;
				if(statsDateRangeType==1){
					startDateTime=DateUtils.truncate(new Date(),Calendar.MONTH);
					endDateTime=null;
				}else if(statsDateRangeType==2){
					startDateTime=DateUtils.truncate(new Date(),Calendar.YEAR);
					endDateTime=null;
				}else{
					startDateTime=null;
					endDateTime=null;
				}

				ServicesResultObject<PaginatedInfor<PosTransactionData>> servicesResultObjectOfgetPosTransactionDataListBy=this.posTradeServices.getPosTransactionDataListBy(DataBaseUser.MAIN_USER, branchNo, terminalMerchantNo,cardNo,startDateTime, endDateTime, nonDataPaginatedInfor);
				
	    	    if(!servicesResultObjectOfgetPosTransactionDataListBy.isSuccess()){
	        		commonResponseDataJson.setSuccess(servicesResultObjectOfgetPosTransactionDataListBy.isSuccess());
	           		commonResponseDataJson.setMsg(servicesResultObjectOfgetPosTransactionDataListBy.getResultMsg());
	        		commonResponseDataJson.setData(null);
	        	}else{
	        		PaginatedInfor<PosTransactionData> posTransactionDataPageInfor=servicesResultObjectOfgetPosTransactionDataListBy.getReturnObj();
	         		  
	        		List<PosTransactionData> currrentPagePosTransactionDataList=posTransactionDataPageInfor.getDataList();
	        		int totalRecords=posTransactionDataPageInfor.getTotalRecords();
	        		int totalPages=1;
	    			if(totalRecords%pageSize!=0){
						totalPages=totalRecords/pageSize+1;
					}else{
						totalPages=totalRecords/pageSize;
					}
	    			
	    			if(startPage>totalPages){
	    				startPage=totalPages;
	    			}
	    			
	        		commonResponseDataJson.setMsg("");
	        		commonResponseDataJson.setSuccess(true);
	        		commonResponseDataJson.setResultCode(JsonResponseData.SUCCESS);
	        		commonResponseDataJson.setData(currrentPagePosTransactionDataList);
	        		
	        		commonResponseDataJson.setRecordsPer(pageSize);
	        		commonResponseDataJson.setPageId(startPage);
	        		commonResponseDataJson.setTotalRecords(totalRecords);
	        		commonResponseDataJson.setTotalPage(totalPages);
	        		
	          		log.debug("getETLJobEfforts json data22:"+JSONObject.fromObject(commonResponseDataJson,JSONObjectHelp.getJsonConfig()).toString());
	
	        	}
    		
    		}
    	}catch(Exception exp){
    		exp.printStackTrace();
    		commonResponseDataJson.setResultCode(JsonResponseData.EXCEPTION);
    		commonResponseDataJson.setMsg(commonResponseDataJson.getMsg()+exp);
    	}
		
		return SUCCESS;
	}
	
	
	
}
